package org.jeecg.modules.manager.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.jeecg.common.util.RedisUtil;
import org.jeecg.common.util.UUIDGenerator;
import org.jeecg.modules.manager.entity.Car;
import org.jeecg.modules.manager.entity.CarInfo;
import org.jeecg.modules.manager.entity.CarInfoVo;
import org.jeecg.modules.manager.entity.DeletedEnum;
import org.jeecg.modules.manager.mapper.CarInfoMapper;
import org.jeecg.modules.manager.mapper.CarMapper;
import org.jeecg.modules.manager.service.ICarService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.Optional;
import java.util.concurrent.atomic.AtomicReference;

/**
 * @Description: 汽车表
 * @Author: jeecg-boot
 * @Date:   2021-11-19
 * @Version: V1.0
 */
@Service
public class CarServiceImpl extends ServiceImpl<CarMapper, Car> implements ICarService {

    @Autowired
    CarMapper carMapper;

    @Autowired
    CarInfoMapper carInfoMapper;

    @Autowired
    RedisUtil redisUtil;

    @Value("${jeecg.path.upload}")
    String path;
    @Transactional(rollbackFor=Exception.class)
    @Override
    public int removeIds(String ids) {
        try{
            //删除汽车表的
            Arrays.stream(ids.split(",")).forEach(id -> {
                        Car car = Car.builder().carId(id).deleted(DeletedEnum.TRUE).build();
                        carMapper.updateById(car);
                        //删除汽车信息表的信息
                        QueryWrapper<CarInfo> carInfoQW = new QueryWrapper<>();
                        carInfoMapper.selectList(carInfoQW.eq("parent_id",id)).stream().forEach(carInfo -> {
                            //设置信息为true
                            carInfo.setDeleted(DeletedEnum.TRUE);
                            carInfoMapper.updateById(carInfo);
                        });
                    }
            );
            //删除缓存
            Arrays.stream(ids.split(",")).forEach(id -> {
                if (redisUtil.get(id) != null){
                    redisUtil.del(id);
                }
            });
            return 1;
        }catch (Exception e){
            e.printStackTrace();
            return -1;
        }
    }

    @Override
    @Transactional
    public int removeCarInfo(String ids) {
        int flag = carInfoMapper.updateById(new CarInfo().setInfoId(ids).setDeleted(DeletedEnum.TRUE));
        String parentId = carInfoMapper.selectById(ids).getParentId();
        Integer count = carInfoMapper.selectCount(new QueryWrapper<CarInfo>().eq("parent_id", parentId).eq("deleted", DeletedEnum.FALSE));
        if (count==0){
            Car car = Car.builder().carId(parentId).deleted(DeletedEnum.TRUE).build();
            carMapper.updateById(car);
        }
        return flag;
    }

    @Override
    public CarInfoVo queryCarById(String id) {
        CarInfoVo vo = new CarInfoVo();
        Car car = carMapper.selectById(id);
        BeanUtils.copyProperties(car,vo);
        vo.setCarInfos(carInfoMapper.getCarInfoListVos(id));
        return vo;
    }

    @Transactional(rollbackFor=Exception.class)
    @Override
    public Car getCarById(String id) {
        QueryWrapper<CarInfo> queryWrapper = new QueryWrapper<CarInfo>();
        queryWrapper.eq("parent_id",id);
        Car car = carMapper.selectById(id);
        car.setCarInfoList(carInfoMapper.selectList(queryWrapper));
        return car;
    }
    @Transactional(rollbackFor=Exception.class)
    @Override
    public String uploadImage(MultipartFile multipartFile,String carInfoId) {
        AtomicReference<String> filePath = new AtomicReference<>();
        Optional.ofNullable(multipartFile).ifPresent(file->{
            try {
                //获取文件是从哪里上传的
                int begin = file.getOriginalFilename().indexOf(".");
                int last = file.getOriginalFilename().length();
                //获得文件后缀名
                String fileFormat = file.getOriginalFilename().substring(begin, last);
                multipartFile.transferTo(new File(path+carInfoId+fileFormat));
                filePath.set(path + UUIDGenerator.generate() + fileFormat);
            } catch (IOException e) {
                e.printStackTrace();
            }
        });
        return filePath.get();
    }
}
